package diona.auth

import net.liftweb.util.Props
import net.liftweb.http._
/**
 * User: anton
 * Date: 23.04.11 1:00
 */
trait Authorizer
{
  lazy val backRedirectUrl = "http://" + Props.get("baseAddress").get + "/" + authAddress.mkString("/")

  @volatile var requestAuthorization: (String) => Nothing = (backUrl) => {
    setBackUrl(backUrl)
    S.redirectTo(buildRedirectUrl())
  }

  def buildRedirectUrl(): String

  val authAddress: List[String]

  @volatile var processToken: LiftRules.DispatchPF

  @volatile var redirectToAuthorization: LiftRules.DispatchPF

  def fetchBackUrl(): String =
  {
    val result: String = backUrlHolder.get
    backUrlHolder.remove()
    result
  }

  def setBackUrl(url: String) {backUrlHolder(url)}

  protected def createCloseWindowResponse(): LiftResponse =
  {
    val req = S.request.get
    val session = S.session.get
    val template = TemplateFinder.findAnyTemplate("templates-hidden" :: "closePopup" :: Nil)
    session.processTemplate(template, req, req.path, 200).get
  }

  private val backUrlHolder = new BackUrl

  private class BackUrl extends SessionVar[String](null)

}